import java.io.*;

/**
 * Created by asd73 on 2017/8/17.
 */
public class IOTest {
    public static void main(String[] args) throws Exception {
        String str = "中国人";
		FileOutputStream fos  = new FileOutputStream("1.txt");

		fos.write(str.getBytes("UTF-8"));
		fos.close();

		FileWriter fw = new FileWriter("2.txt");
		fw.write(str);
		fw.close();
        PrintWriter pw = new PrintWriter("1.txt","utf-8");
        pw.write(str);
        pw.close();

/*		FileReader fr = new FileReader("1.txt");
		char[] buf = new char[1024];
		int len = fr.read(buf);
		String myStr = new String(buf,0,len);
		System.out.println(myStr);*/
		FileInputStream fr = new FileInputStream("1.txt");
		byte[] buf = new byte[1024];
		int len = fr.read(buf);
		String myStr = new String(buf,0,len,"UTF-8");
		System.out.println(myStr);
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("1.txt"),"UTF-8"));
        String myStr1 = br.readLine();
        br.close();
        System.out.println(myStr1);
    }

}

class IOTest2{
	public static void main(String[] args) throws IOException {
		String s = "中国\r\n中国1";
		FileOutputStream fileOutputStream = new FileOutputStream("a.txt");
		fileOutputStream.write(s.getBytes("gbk"));
		fileOutputStream.close();

		FileInputStream inputStream = new FileInputStream("a.txt");
		byte[] buf = new byte[1024];
		int len =inputStream.read(buf);
		String myStr = new String(buf,0,len,"utf-8");
		System.out.println(myStr+" 1");
		inputStream.close();
		//System.out.println("测试");

		OutputStream outputStream = new FileOutputStream("b.txt");
		outputStream.write(s.getBytes("gbk"));
		outputStream.close();

		InputStream inputStream1 =new FileInputStream("b.txt");
		byte[] inbyte = new byte[1024];
		int len1 = inputStream1.read(inbyte);
		String mystr1 = new String(inbyte,0,len1,"utf-8");
		System.out.println(mystr1+" 2");inputStream1.close();

		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("b.txt")));
		StringBuffer buffer = new StringBuffer();
		String s2 = "";
		while ((s2 = reader.readLine())!=null){
			buffer.append(s2).append("\n");
		}
		reader.close();
		System.out.println(buffer+" 3");

		FileReader fileReader = new FileReader("b.txt");
		FileWriter fileWriter = new FileWriter("c.txt",true);
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
		String line = "";
		while ((line = bufferedReader.readLine())!=null){
			bufferedWriter.write(line);
			bufferedWriter.newLine();
			bufferedWriter.flush();
		}
	}
}

class IOTest3{
	public static void main(String[] args) throws IOException {
		FileReader fileReader = new FileReader("c.txt");
		FileWriter fileWriter = new FileWriter("dd.txt");
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
		String ling="";
		while ((ling=bufferedReader.readLine())!=null){
			bufferedWriter.write(ling);
			bufferedWriter.newLine();
			bufferedWriter.flush();
		}
		bufferedReader.close();
		bufferedWriter.close();
		test();
	}
	public static void test() throws IOException{
		InputStream inputStream = new FileInputStream("c.txt");
		byte[] bytes = new byte[1024];
		int leng = inputStream.read(bytes);

		String s =new String(bytes,0,leng,"utf-8");
		System.out.println(s);
	}
}
